//
//  EditPhotoCategoryView.m
//  BabyHelper
//
//  Created by  thomas on 11-8-27.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "EditPhotoAlbumView.h"


@implementation EditPhotoAlbumView
@synthesize nameLabel,nameTextField,datePicker;
@synthesize currentPhotoAlbumEntry;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.title = NSLocalizedString(@"Edit",@"");
		
	self.nameLabel.text            = NSLocalizedString(@"Album Name",@"");
	self.nameTextField.placeholder = NSLocalizedString(@"Please input album name...",@"");
		
	UIBarButtonItem *saveItem = [[UIBarButtonItem alloc]
								 initWithTitle:NSLocalizedString(@"Done",@"")
								 style:UIBarButtonItemStyleBordered 
								 target:self
								 action:@selector(toggleSave)		
								 ];
	self.navigationItem.rightBarButtonItem = saveItem;
	[saveItem release];
	
	//set background image
	self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
	self.view.backgroundColor = [UIColor clearColor];
	//set navigation background color
	self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:navBarRed green:navBarGreen blue:navBarBlue alpha:navBarAlpha];
	
	if(self.currentPhotoAlbumEntry != nil )
	{
		self.nameTextField.text = self.currentPhotoAlbumEntry.name;
		[self.datePicker setDate:self.currentPhotoAlbumEntry.timestamp];
	} 
	else { 
		[self.datePicker setDate:[NSDate date] animated:YES];
	}	
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


-(IBAction)backgroundTap:(id)sender{
	[nameTextField resignFirstResponder];
}

-(IBAction)toggleSave{
	if([self DataValidation] == YES)
    {
		[self SavePhotoAlbumEntry];
		[self.navigationController popViewControllerAnimated:true];		
	}
}

-(BOOL)DataValidation{
	
	if([self.nameTextField.text length] == 0)
	{
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Alert",@"")
														message:NSLocalizedString(@"Name should not be null",@"")
													   delegate:nil 
											  cancelButtonTitle:@"OK"
											  otherButtonTitles:nil];
		[alert show];
		[alert release];
		return NO;
	}
	
	if([self.nameTextField.text length] > 100)
	{
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Alert",@"")
														message:NSLocalizedString(@"Input text too long",@"")
													   delegate:nil 
											  cancelButtonTitle:@"OK"
											  otherButtonTitles:nil];
		[alert show];
		[alert release];
		return NO;
	}
	return YES;
}


-(void)SavePhotoAlbumEntry{	
	Dao *dao = [Dao getInstance];
	PhotoAlbumEntry *entry = [[PhotoAlbumEntry alloc]init];
	
	entry.timestamp = [self.datePicker date];
	entry.name      =  self.nameTextField.text;
	
	if(self.currentPhotoAlbumEntry == nil )
	{
		[dao InsertPhotoCategoryEntry:entry];
	}else {
		entry.index = self.currentPhotoAlbumEntry.index;
		[dao UpdatePhotoCategoryEntry:entry];
	}
	[entry release];	
}


@end
